package cn.iocoder.yudao.module.student.controller.app.api;

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.string.StrUtils;
import cn.iocoder.yudao.module.student.api.student.StudentApi;
import cn.iocoder.yudao.module.student.api.student.dto.StudentUpdateReqDTO;
import cn.iocoder.yudao.module.student.api.student.dto.WebStudentPageReqDTO;
import cn.iocoder.yudao.module.student.api.student.dto.WebStudentRespDTO;
import cn.iocoder.yudao.module.student.controller.app.student.vo.WebStudentPageReqVO;
import cn.iocoder.yudao.module.student.controller.app.student.vo.WebStudentUpdateReqVO;
import cn.iocoder.yudao.module.student.convert.student.WebStudentConvert;
import cn.iocoder.yudao.module.student.dal.dataobject.student.WebStudentDO;
import cn.iocoder.yudao.module.student.service.student.WebStudentService;
import cn.iocoder.yudao.module.system.api.message.MessageApi;
import cn.iocoder.yudao.module.system.api.message.dto.MessageCreateReqDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.regex.Pattern;

@Service
@Validated
public class StudentApiImpl implements StudentApi {
    @Resource
    private WebStudentService studentService;
    @Resource
    private MessageApi messageApi;

    @Value("${spring.profiles.active}")
    private String active;
    @Override
    public Boolean updateStudent(StudentUpdateReqDTO studentUpdateReqDTO) {
        WebStudentUpdateReqVO webStudentUpdateReqVO=new WebStudentUpdateReqVO();
        BeanUtil.copyProperties(studentUpdateReqDTO,webStudentUpdateReqVO);
        studentService.updateWebStudent(webStudentUpdateReqVO);
        return true;
    }

    @Override
    public Boolean isAuthorizedId(String phone) {
        WebStudentDO studentDO=studentService.getWebStudent(phone);
        studentDO.setIsRealName("yes");
        WebStudentUpdateReqVO webStudentUpdateReqVO=new WebStudentUpdateReqVO();
        BeanUtil.copyProperties(studentDO,webStudentUpdateReqVO);
        studentService.updateWebStudent(webStudentUpdateReqVO);
        if(StringUtils.isNotBlank(studentDO.getMobile())){
            boolean isMatch= StrUtils.validMobile(studentDO.getMobile());
            if(!isMatch){
                String title;
                String message;
                if("1".equals(studentDO.getStatus())){
                    title="身份认证成功";
                    message="恭喜，您已身份认证成功，可以愉快地投递简历拉~";
                }else{
                    title="身份认证失败";
                    if(StringUtils.isNotBlank(active) && "prod".equals(active)){
                        message="身份认证失败，失败原因为："+studentDO.getRemark()+"，请重新进行身份实名认证：<a href='https://www.datarer.com/auditId'  target='_blank' >https://www.datarer.com/auditId</a>";
                    }else{
                        message="身份认证失败，失败原因为："+studentDO.getRemark()+"，请重新进行身份实名认证：<a href='http://47.107.248.245/test/#/auditId'  target='_blank'>http://47.107.248.245/test/#/auditId</a>";
                    }
                }
                sendmessage(1L,studentDO.getId(),title,message);
            }
        }
        return true;
    }
    @Async
    void sendmessage(Long type, Long id, String title, String messageText){
        MessageCreateReqDTO messageCreateReqVO=new MessageCreateReqDTO();
        messageCreateReqVO.setPhone("");
        messageCreateReqVO.setType(type);
        messageCreateReqVO.setStatus("5");
        messageCreateReqVO.setStudentId(id);
        messageCreateReqVO.setStudentName("系统管理员");
        messageCreateReqVO.setRemark(messageText);
        messageCreateReqVO.setTitle(title);
        messageApi.sendMessage(messageCreateReqVO);
    }
    @Override
    public PageResult<WebStudentRespDTO> getWebStudentPage(WebStudentPageReqDTO pageReqVO) {
        WebStudentPageReqVO webStudentPageReqVO=new WebStudentPageReqVO();
        BeanUtil.copyProperties(pageReqVO,webStudentPageReqVO);
        PageResult<WebStudentDO> result= studentService.getWebStudentPage(webStudentPageReqVO);
        return WebStudentConvert.INSTANCE.convertPage1(result);
    }

    @Override
    public List<WebStudentRespDTO> getWebStudentByIds(List<Long> ids) {
        return WebStudentConvert.INSTANCE.convertList1(studentService.getWebStudentList(ids));
    }
}
